// bookgo/dsh/ac_detail/ac_detail.js

var config = require('../../../config');
var WxParse = require('../../../dist/wxParse/wxParse.js');

var get_ac_url = config.dsh.salons.get_ac_url;
var token = config.dsh.token;

var get_member_url = config.dsh.members.get_member_url;

var send_template_url = config.user.send_template_url;

let get_openid_url = config.user.get_openid_url;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ac:[],
    user:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.get_ac(options.id);
    this.get_ac(2);
  },
  get_ac(id){
    const _this = this;
    wx.request({
      url: get_ac_url + id + '?token=' + token,
      success:function(res){
        console.log(res.data);
        var member_id = res.data.member_id;
        _this.get_user(member_id);
        _this.setData({
          ac:res.data
        });
        WxParse.wxParse('ac_detail', 'html', res.data.content, _this, 0);
      }
    });
  },
  get_user(id){
    const _this = this;
    wx.request({
      url: get_member_url + id + '?token=' + token,
      success: function (res) {
        console.log(res.data);
        _this.setData({
          user:res.data
        });
      }
    });
  },
  sendTemplate: function(e){
    let formId = e.detail.formId;
    console.log('触发了submit事件，推送码是：' + formId);
    let _this = this;
    // 调用登录接口
    var openid = wx.getStorageSync('openid');
    if (!openid) {
      wx.login({
        success: function (e) {
          wx.request({
            url: config.service.loginUrl,
            data: {
              code: e.code,
            },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              console.log('获取openid');
              console.log(res.data)
              wx.setStorageSync('openid', res.data.openid)
              // 连接服务器
              _this.doSendTemplate(res.data.openid, formId);
            },
            fail: function (res) {
              console.log(res)
            }
          })
          console.log('wxlogin successd........');
        }
      });
    } else {
      _this.doSendTemplate(openid,formId);
    }
    
  },
  doSendTemplate: function (openid,formId){
    wx.request({
      url: send_template_url,
      method:'POST',
      data:{
        openid:openid,
        form_id:formId
      },
      success:function(res){
        console.log(res);
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})